package com.chat.common.user.consumer;

import com.chat.common.common.constant.MQConstant;
import com.chat.common.common.domain.dto.PushMessageDTO;
import com.chat.common.websocket.domain.enums.WSPushTypeEnum;
import com.chat.common.websocket.service.WebSocketService;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(consumerGroup = MQConstant.PUSH_GROUP, topic = MQConstant.PUSH_TOPIC,messageModel = MessageModel.BROADCASTING)
public class PushConsumer implements RocketMQListener<PushMessageDTO> {
    @Resource
    private WebSocketService webSocketService;
    @Override
    public void onMessage(PushMessageDTO message) {
        WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(message.getPushType());
        switch (wsPushTypeEnum) {
            case ALL:
                webSocketService.sendToAllOnline(message.getWsBaseMsg(), null);
                break;
            case USER:
                //对部分用户进行消息推送
                message.getUidList().forEach(uid -> {
                   webSocketService.sendToUid(message.getWsBaseMsg(),uid);
                });
                break;
        }
    }
}
